package com.levi.login.web;

import com.levi.login.dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

    /**
     * @author
     * @Date 2021/4/22
     */
    public class LoginServlet extends HttpServlet {

        private final UserDAO userDAO = new UserDAO();

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            boolean exist = userDAO.exist(username, password);
            if (exist) {
                System.out.println("登陆成功");

                response.sendRedirect(request.getContextPath() + "/main.jsp");
            } else {
                System.out.println("登陆失败");
                request.setAttribute("msg", "用户名或密码错误");
                request.getRequestDispatcher(request.getContextPath() + "index.jsp").forward(request, response);
            }
        }
    }


